<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of smooth</title>
  <meta name="keywords" content="smooth">
  <meta name="description" content="SMOOTH Perform windowed smoothing on a vector using mathematical functions">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2005 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
  <script type="text/javascript">
    if (top.frames.length == 0) { top.location = "../../index.html"; };
  </script>
</head>
<body>
<a name="_top"></a>
<!-- ../menu.html trunk --><!-- menu.html utils -->
<h1>smooth
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>SMOOTH Perform windowed smoothing on a vector using mathematical functions</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>function y = smooth(x,frame,mode) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment"> SMOOTH Perform windowed smoothing on a vector using mathematical functions
 
 SYNTAX
 
     Y = smooth(X,FRAME)
     Y = smooth(X,FRAME,MODE)
 
 
 DESCRIPTION
 
 Y = smooth(X,FRAME) smooths the input vector X by calculating the running
 RMS over a series of frames. FRAME specifies the frame characteristics;
 it can be set to:
 
 a scalar - this will be used as the length of the frame, the window will
 be rectangular
 a vector - this specifies the shape of the analysis window, the frame
 length will be length(frame).
 
 Y = smooth(X,FRAME,MODE) allows the user to specify a different
 mathematical smoothing function. The options are:
 
 'rms' - calculates the running rms (default)
 'mean' - calculates the running mean (moving average filter)
 'median' - calculates the running median
 
 NOTE: SMOOTH uses a vectorized implementation that may be slow when X 
 and/or FRAME_LENGTH are very large. The number of elements that are used
 for calculation is length(X)*FRAME_LENGTH. The algorithm vectorizes the
 operation by creating a matrix of indexes and extracting its diagonals.
 E.g. for a vector of length 4 and frame_length of 2, the algorithm
 creates a temporary zero-padded matix x2 from which it creates a set of
 indexes:
 
 1 1
 2 2
 3 3
 4 4
 5 5
 6 6
 
 It then extracts the diagonals where -length(x2)+frame_length&lt;=k&lt;=0,
 yielding:
 
 1 2
 2 3
 3 4
 4 5
 
 this is used to index x2; operations are then perfromed along the rows.</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
</ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
</ul>
<!-- crossreference -->



<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function y = smooth(x,frame,mode)</a>
0002 
0003 <span class="comment">% SMOOTH Perform windowed smoothing on a vector using mathematical functions</span>
0004 <span class="comment">%</span>
0005 <span class="comment">% SYNTAX</span>
0006 <span class="comment">%</span>
0007 <span class="comment">%     Y = smooth(X,FRAME)</span>
0008 <span class="comment">%     Y = smooth(X,FRAME,MODE)</span>
0009 <span class="comment">%</span>
0010 <span class="comment">%</span>
0011 <span class="comment">% DESCRIPTION</span>
0012 <span class="comment">%</span>
0013 <span class="comment">% Y = smooth(X,FRAME) smooths the input vector X by calculating the running</span>
0014 <span class="comment">% RMS over a series of frames. FRAME specifies the frame characteristics;</span>
0015 <span class="comment">% it can be set to:</span>
0016 <span class="comment">%</span>
0017 <span class="comment">% a scalar - this will be used as the length of the frame, the window will</span>
0018 <span class="comment">% be rectangular</span>
0019 <span class="comment">% a vector - this specifies the shape of the analysis window, the frame</span>
0020 <span class="comment">% length will be length(frame).</span>
0021 <span class="comment">%</span>
0022 <span class="comment">% Y = smooth(X,FRAME,MODE) allows the user to specify a different</span>
0023 <span class="comment">% mathematical smoothing function. The options are:</span>
0024 <span class="comment">%</span>
0025 <span class="comment">% 'rms' - calculates the running rms (default)</span>
0026 <span class="comment">% 'mean' - calculates the running mean (moving average filter)</span>
0027 <span class="comment">% 'median' - calculates the running median</span>
0028 <span class="comment">%</span>
0029 <span class="comment">% NOTE: SMOOTH uses a vectorized implementation that may be slow when X</span>
0030 <span class="comment">% and/or FRAME_LENGTH are very large. The number of elements that are used</span>
0031 <span class="comment">% for calculation is length(X)*FRAME_LENGTH. The algorithm vectorizes the</span>
0032 <span class="comment">% operation by creating a matrix of indexes and extracting its diagonals.</span>
0033 <span class="comment">% E.g. for a vector of length 4 and frame_length of 2, the algorithm</span>
0034 <span class="comment">% creates a temporary zero-padded matix x2 from which it creates a set of</span>
0035 <span class="comment">% indexes:</span>
0036 <span class="comment">%</span>
0037 <span class="comment">% 1 1</span>
0038 <span class="comment">% 2 2</span>
0039 <span class="comment">% 3 3</span>
0040 <span class="comment">% 4 4</span>
0041 <span class="comment">% 5 5</span>
0042 <span class="comment">% 6 6</span>
0043 <span class="comment">%</span>
0044 <span class="comment">% It then extracts the diagonals where -length(x2)+frame_length&lt;=k&lt;=0,</span>
0045 <span class="comment">% yielding:</span>
0046 <span class="comment">%</span>
0047 <span class="comment">% 1 2</span>
0048 <span class="comment">% 2 3</span>
0049 <span class="comment">% 3 4</span>
0050 <span class="comment">% 4 5</span>
0051 <span class="comment">%</span>
0052 <span class="comment">% this is used to index x2; operations are then perfromed along the rows.</span>
0053 
0054 <span class="comment">%% Gather inputs</span>
0055 
0056 <span class="keyword">if</span> ~isvector(x)
0057     error(<span class="string">'''x'' must be a vector'</span>)
0058 <span class="keyword">end</span>
0059 
0060 <span class="keyword">if</span> isscalar(frame)
0061     frame_length = frame;
0062     window = ones(frame_length,1);
0063 <span class="keyword">elseif</span> isvector(frame)
0064     window = frame;
0065     frame_length = length(frame);
0066 <span class="keyword">else</span>
0067     error(<span class="string">'''frame'' must be a vector or a scalar'</span>)
0068 <span class="keyword">end</span>
0069 
0070 <span class="keyword">if</span> nargin&lt;3
0071     mode = <span class="string">'rms'</span>;
0072 <span class="keyword">end</span>
0073 
0074 <span class="comment">%% Smooth</span>
0075 
0076 <span class="comment">% zero pad</span>
0077 x2 = [zeros(ceil((frame_length)/2)-1,1); x(:); zeros(floor(frame_length/2),1)];
0078 
0079 <span class="comment">% get indexes</span>
0080 index = spdiags(repmat((1:length(x2))',[1 frame_length]),0:-1:-length(x2)+frame_length);
0081 
0082 window = repmat(window,[1 length(x)]);
0083 
0084 <span class="comment">% do calculations</span>
0085 <span class="keyword">switch</span> lower(mode)
0086     <span class="keyword">case</span> <span class="string">'rms'</span>
0087         y = sqrt(mean((window.*x2(index)).^2));
0088     <span class="keyword">case</span> <span class="string">'median'</span>
0089         y = median((window.*x2(index)));
0090     <span class="keyword">case</span> <span class="string">'mean'</span>
0091         y = mean((window.*x2(index)));
0092     <span class="keyword">otherwise</span>
0093         error(<span class="string">'Unknown ''mode'' specified'</span>)
0094 <span class="keyword">end</span>
0095 
0096 <span class="comment">% transpose if necessary</span>
0097 <span class="keyword">if</span> size(y,1)~=size(x,1)
0098     y = y';
0099 <span class="keyword">end</span></pre></div>
<hr><address>Generated on Tue 30-Apr-2013 10:18:12 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" target="_parent">m2html</a></strong> &copy; 2005</address>
</body>
</html>